package com.ssy.lingxi.report.dao.aftersale;

import com.ssy.lingxi.report.model.bo.tally.statistics.AfterSaleRepairApplyStatisticsTallyVO;
import com.ssy.lingxi.report.model.bo.tally.statistics.AfterSaleRepairHandleStatisticsTallyVO;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 售后维修DAO
 * @author 伍将
 * @date 2020/11/14
 * @version 2.0.0
 */
@Service
public class RepairGoodsDAO {

    @Resource
    @Qualifier("afterSaleJdbcTemplate")
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询售后维修申请统计数量
     * @return
     */
    public List<AfterSaleRepairApplyStatisticsTallyVO> getApplyStatisticsCount() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" member_id,role_id,")
                .append(" sum(case when outer_status in(1,3) then 1 else 0 end) toBeCommitCount,")
                .append(" sum(case when outer_status = 4 then 1 else 0 end) toBeConfirmCompleteCount")
                .append(" FROM as_repair_goods ")
                .append(" group by member_id,role_id;");

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AfterSaleRepairApplyStatisticsTallyVO.class));
    }

    /**
     * 查询售后维修处理统计数量
     * @return
     */
    public List<AfterSaleRepairHandleStatisticsTallyVO> getHandStatisticsCount() {
        StringBuilder sql = new StringBuilder();
        sql.append(" select ")
                .append(" parent_member_id member_id,parent_member_role_id role_id,")
                .append(" sum(case when inner_status = 1 then 1 else 0 end) toBeCommitValifyCount,")
                .append(" sum(case when inner_status = 2 then 1 else 0 end) toBeValifyStep1Count,")
                .append(" sum(case when inner_status = 3 then 1 else 0 end) toBeValifyStep2Count,")
                .append(" sum(case when inner_status in(4,5,9,10) then 1 else 0 end) toBeConfirmCount")
                .append(" FROM as_repair_goods ")
                .append(" where outer_status>1")  // 外部已提交
                .append(" group by parent_member_id,parent_member_role_id;");

        return jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(AfterSaleRepairHandleStatisticsTallyVO.class));
    }
}
